package com.cskaoyan.weixin.service;

import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.bean.User1;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.weixin.bean.coupon.Coupon;
import com.cskaoyan.weixin.bean.coupon.CouponSelectListVO;
import com.cskaoyan.weixin.bean.coupon.CouponidBO;
import com.cskaoyan.weixin.bean.coupon.DataBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImpl implements  CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    @Override
    public Coupon couponlist(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Coupon coupon=new Coupon();
        Integer count=couponMapper.couponCount();
        List<DataBean> dataBeans=couponMapper.selectCouponwx();
        //PageInfo pageInfo=new PageInfo(dataBeans);
        coupon.setCount(count);
        coupon.setData(dataBeans);

        return coupon;
    }

    @Override
    public Coupon couponmylist(Integer status, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Coupon coupon=new Coupon();
        Integer count=couponUserMapper.couponCount();
        User1 primaryPrincipal = (User1) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        List<DataBean> dataBeans=couponUserMapper.selectCoupon(status, primaryPrincipal.getId());
        coupon.setData(dataBeans);
        coupon.setCount(count);
        return coupon;
    }

    @Override
    public List<CouponSelectListVO> couponselectlist(Integer cartId, Integer grouponRulesId) {
        Coupon coupon=new Coupon();
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        User1 user = (User1) principals.getPrimaryPrincipal();
        List<CouponSelectListVO> couponSelectListVOS=couponMapper.selectList(cartId,user.getId());
        coupon.setData(couponSelectListVOS);
        return couponSelectListVOS;

    }



    @Override
    public Integer receive(Integer couponid) {
        Subject subject= SecurityUtils.getSubject();
        User1 user1=(User1) subject.getPrincipal();
        Integer userId=user1.getId();
//        CouponUserExample couponUserExample=new CouponUserExample();
//        couponUserExample.createCriteria().andCouponIdEqualTo(couponid);
        com.cskaoyan.bean.Coupon coupon = couponMapper.selectByPrimaryKey(couponid);
        Date date=new Date();
        CouponUser couponUser=new CouponUser();
        couponUser.setOrderId(0);
        couponUser.setStatus(0);
        couponUser.setCouponId(couponid);
        couponUser.setUserId(userId);
        couponUser.setAddTime(date);
        couponUser.setDeleted(false);
        if (coupon.getDays() != 0) {
            long dateTime = date.getTime() + coupon.getDays() * 24 * 3600 * 1000;
            Date date1 = new Date(dateTime);
            couponUser.setStartTime(date);
            couponUser.setEndTime(date1);
        }else {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
            Date parse = null;
            try {
                parse = simpleDateFormat.parse(coupon.getStartTime());
                couponUser.setStartTime(parse);
                parse = simpleDateFormat.parse(coupon.getEndTime());
                couponUser.setEndTime(parse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //couponUser.setEndTime(date);
        //couponUser.setStartTime(date);
       // couponUser.setUsedTime(date);
        couponUser.setUpdateTime(date);
        couponUserMapper.insert( couponUser);
        return 1;
    }
    @Override
    public Integer exchange(String code) {
        Subject subject= SecurityUtils.getSubject();
        User1 user1=(User1) subject.getPrincipal();
        Integer userId=user1.getId();
        CouponExample couponExample = new CouponExample();
        couponExample.createCriteria().andCodeEqualTo(code).andStatusEqualTo((short)0);
        List<com.cskaoyan.bean.Coupon> coupons = couponMapper.selectByExample(couponExample);
        if (coupons.size() == 0) {
            return 20000;
        }
        com.cskaoyan.bean.Coupon coupon = coupons.get(0);
        Short limit = coupon.getLimit();
       if (limit==1){
            CouponUserExample couponUserExample = new CouponUserExample();
            couponUserExample.createCriteria().andCouponIdEqualTo(coupon.getId()).andUserIdEqualTo(userId);
            long l = couponUserMapper.countByExample(couponUserExample);
            if (l!=0)return 70000;
        }
        CouponUser couponid = new CouponUser();
        couponid.setUserId(userId);
        Date date=new Date();
        couponid.setAddTime(date);
        couponid.setDeleted(false);
//        if (coupon.getStartTime() !=null) {
//            couponid.setStartTime(coupon.getStartTime());
//
//        }
        //couponid.setEndTime(date);
        //couponid.setStartTime(date);
        if (coupon.getDays() != 0) {
            couponid.setStartTime(date);
           Long endTimeLong = date.getTime() + coupon.getDays() * 24 * 3600 * 1000;
            Date endTimes = new Date(endTimeLong);
            couponid.setEndTime(endTimes);
        }else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = null;
            try {

                parse = dateFormat.parse(coupon.getEndTime());
                couponid.setEndTime(parse);
                parse = dateFormat.parse(coupon.getStartTime());
                couponid.setStartTime(parse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        //couponid.setUsedTime(date);
        couponid.setUpdateTime(date);
        couponid.setCouponId(coupon.getId());
        couponUserMapper.insertSelective(couponid);
        return 1;
    }


}
